iT邦幫忙

2021 iThome 鐵人賽

DAY 19
0

在我們寫Service時,我們通常會記錄自己想要的logs以供分析,本篇來介紹如何使用Odoo的log。

我們於controllers/main.xml 內匯入 logging,並增加log:

import logging
_logger = logging.getLogger(__name__)

class StudtentController(Controller):
    
	@route('/student', methods=['POST'], type='json', auth='public', cors='*', csrf=False)
	    def create_student(self):
	        _logger.info('---------> %s \n', request.httprequest.data)
	        student_data = json.loads(request.httprequest.data)
	        val = {
	            'name': student_data.get('name'),
	            'nickname': student_data.get('nickname'),
	            'gender': student_data.get('gender'),
	            'birthday': student_data.get('birthday')
	        }
	
	
	        student_obj = request.env['res.student'].sudo()
	        student_obj.create(val)
	        result = {'code': 200, 'message': 'Created Successfully'}
	        body = json.dumps(result, default=date_utils.json_default)
	        return Response(
	            body, status=200,
	            headers=[('Content-Type', 'application/json'), ('Content-Length', len(body))]
	        )

我們import loggin,並且將它初始化

import logging
_logger = logging.getLogger(__name__)

印出自己想要的層級與字串

_logger.info('---------> %s \n', request.httprequest.data)

Log level

logs有分五個層級,infodebugerrorwarningcritical,寫法就如同上方一樣

寫好以後別忘了在odoo.conf內設定路徑,否則只有在terminal才會看到

logfile = /var/log/odoo/odoo.log
...

如此一來,當我們打API的時候便會顯示log,並會儲存於指定檔案位置:

request body:

{
	"name": "Andy Chen",
	"nickname": "Andy",
	"gender": "male",
	"birthday": "2020-02-02"
}

log:

2021-10-02 17:52:04,987 1630 INFO odoo.service werkzeug: 127.0.0.1- - [02/Oct/2021 17:52:04] "POST /longpolling/poll HTTP/1.0" 200 - 19 0.015 0.059
2021-10-02 17:52:55,080 1630 INFO odoo.service werkzeug: 127.0.0.1 - - [02/Oct/2021 17:52:55] "POST /longpolling/poll HTTP/1.0" 200 - 7 0.002 0.006
2021-10-02 17:53:45,184 1630 INFO odoo.service werkzeug: 127.0.0.1 - - [02/Oct/2021 17:53:45] "POST /longpolling/poll HTTP/1.0" 200 - 7 0.002 0.006
2021-10-02 17:54:35,275 1630 INFO odoo.service werkzeug: 127.0.0.1 - - [02/Oct/2021 17:54:35] "POST /longpolling/poll HTTP/1.0" 200 - 7 0.003 0.007
2021-10-02 17:54:52,248 1630 INFO odoo.service odoo.addons.student.controllers.main: ---------> b' {\n            "name": "Andy Chen",\n            "nickname": "Andy",\n            "gender": "male",\n            "birthday": "2020-02-02"\n            }'

上一篇
Day18 Let's ODOO: Paper Format
下一篇
Day20 Let's ODOO: Scheduled Actions
系列文
Let's ODOO 開發與應用30天挑戰30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言